package com.yanqu.road.entity.cricket.court;

import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class CricketCourtRankListModel {

    private final Object lock = new Object();

    private int groupId;

    private LinkedList<CricketCourtUserData> rankList = new LinkedList<>();

    private int maxRank;

    private Comparator sort;

    public CricketCourtRankListModel(int groupId, int maxRank, Comparator sort) {
        this.groupId = groupId;
        this.maxRank = maxRank;
        this.sort = sort;
    }

    public int getRank(CricketCourtUserData userData) {
        int myRank = -1;
        int rank = 0;
        for (CricketCourtUserData data : rankList) {
            rank++;
            if (data.getServerId() == userData.getServerId() && data.getUserId() == userData.getUserId()) {
                myRank = rank;
                break;
            }
        }
        return myRank;
    }

    /**
     * 获取区间排名的用户
     */
    public List<CricketCourtUserData> getRangeUserList(int start, int end) {
        List<CricketCourtUserData> list = new ArrayList<>();
        int rank = 0;
        for (CricketCourtUserData data : rankList) {
            rank++;
            if (rank < start || rank > end) {
                continue;
            }
            list.add(data);
        }
        return list;
    }

    public void rankChange(CricketCourtUserData userData) {
        boolean change = false;
        synchronized (lock) {
            // 已有数据
            for (CricketCourtUserData tmpData : rankList) {
                if (tmpData.getUserId() == userData.getUserId() ) {
                    if (tmpData.getScore() != userData.getScore()) {
                        tmpData.setScore(userData.getScore());
                        tmpData.setUpdateTime(userData.getUpdateTime());
                    }
                    change = true;
                    break;
                }
            }
            if (!change) {
                // 没满
                if (rankList.size() < maxRank) {
                    rankList.addLast(userData);
                    change = true;
                } else {
                    // 干掉最后一名
                    CricketCourtUserData last = rankList.getLast();
                    if (userData.getScore() > last.getScore()
                            || (userData.getScore() == last.getScore() && userData.getUpdateTime() < last.getUpdateTime())) {
                        rankList.removeLast();
                        rankList.addLast(userData);
                        change = true;
                    }
                }
            }
            if (change) {
                rankList.sort(sort);
            }
        }
    }


    public void setRankList(List<CricketCourtUserData> list) {
        synchronized (lock) {
            rankList = new LinkedList<>(list);
            rankList.sort(sort);
            while (rankList.size() > maxRank) {
                rankList.removeFirst();
            }
        }
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public LinkedList<CricketCourtUserData> getRankList() {
        return rankList;
    }

    public void setRankList(LinkedList<CricketCourtUserData> rankList) {
        this.rankList = rankList;
    }

    public int getMaxRank() {
        return maxRank;
    }

    public void setMaxRank(int maxRank) {
        this.maxRank = maxRank;
    }
}
